<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户登录</title>
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap-theme.min.css" >
    <link rel="icon" href="data:;base64,=">
</head>
<body>
<div class="container-fluid">
    <nav class="navbar navbar-inverse" >
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="/">新闻网</a>
            </div>

            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav" id="navbar-nav">
                </ul>
                <form class="navbar-form navbar-right" action="/?key=">
                    <div class="form-group">
                        <input type="text" class="form-control" name="key" placeholder="请输入要搜索的内容">
                    </div>
                    <button type="submit" class="btn btn-success">搜索</button>
                </form>
            </div>
        </div>
    </nav>
    <div class="row">
        <div class="col-md-12" style="margin: auto">
            <form method="post" action="" id="form" onsubmit="return false" >
                <div class="form-group">
                    <label for="">用户名</label>
                    <input type="text" name="name" class="form-control" id="" placeholder="请输入用户名">
                </div>
                <div class="form-group">
                    <label for="exampleInputPassword1">密码</label>
                    <input type="password" name="password" class="form-control" id="exampleInputPassword1" placeholder="请输入密码">
                </div>
                <button type="submit" class="btn btn-success" id="btn">登录</button>
            </form>
        </div>
    </div>
</div>

</body>
<script src="/static/bootstrap/js/jquery-3.6.0.min.js" ></script>
<script src="/static/bootstrap/js/bootstrap.min.js" ></script>
<script>
    $(function () {
        //获取网站栏目
        $.ajax({
            "url": "/v1/art/category",
            type: "GET",
            dataType: "json",
            success: function (data) {
                var html = '';
                $.each(data.data, function (i, item) {
                    html += "<li><a href=/?category_id=" + item.id + ">" + item.name + "(" + item.count + ")" + "<span class=\"sr-only\">(current)</span></a></li>\n"
                })
                $("#navbar-nav").append(html)
            }
        })

        $("#btn").click(function (){
            $.ajax({
                "url": "/v1/user/login",
                type: "POST",
                dataType: "json",
                data: $("#form").serialize(),
                success: function (data) {
                    //登录成功
                    console.log(data)
                    localStorage.setItem("token",data.data.token)
                    localStorage.setItem("user_name",data.data.user_name)
                    location.href = "/"
                },
                error:function (data){
                   if (data.responseJSON.data){
                       alert(data.responseJSON.data)
                       return false
                   }
                }
            })
        })
    })
</script>
</html>